// $Id: FilterView.java 75 2011-12-30 21:33:56Z huijzer@gmail.com $
package lime.workflow.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;

public class FilterView extends ViewPart {

	public static final String ID = "lime.workflow.view.filter";
	
	// Widgets
	private Button dormantState;
	private Button acquiredState;
	private Button finishedState;
	private Button pausedState;
	private Button dpausedState;
	private Button apausedState;
	private Button ppausedState;
	private Button faultedState;
	
	@Override
	public void createPartControl(Composite parent) {

		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(2, false));
		
		Label runtimeStateLabel = new Label(container, SWT.NONE);
		runtimeStateLabel.setText("Runtime State:");
		GridData layoutData = new GridData();
		layoutData.horizontalSpan = 2;
		runtimeStateLabel.setLayoutData(layoutData);
		
		dormantState = new Button(container, SWT.CHECK);
		dormantState.setSelection(true);
		new Label(container, SWT.NONE).setText("Dormant (0)");
		
		acquiredState = new Button(container, SWT.CHECK);
		acquiredState.setSelection(true);
		new Label(container, SWT.NONE).setText("Acquired (1)");
		
		finishedState = new Button(container, SWT.CHECK);
		finishedState.setSelection(false);
		new Label(container, SWT.NONE).setText("Finished (2)");
		
		pausedState = new Button(container, SWT.CHECK);
		pausedState.setSelection(true);
		new Label(container, SWT.NONE).setText("Paused (3)");
		
		dpausedState = new Button(container, SWT.CHECK);
		dpausedState.setSelection(true);
		new Label(container, SWT.NONE).setText("DPaused (4)");
		
		apausedState = new Button(container, SWT.CHECK);
		apausedState.setSelection(true);
		new Label(container, SWT.NONE).setText("APaused (5)");
		
		ppausedState = new Button(container, SWT.CHECK);
		ppausedState.setSelection(true);
		new Label(container, SWT.NONE).setText("PPaused (6)");
		
		faultedState = new Button(container, SWT.CHECK);
		faultedState.setSelection(true);
		new Label(container, SWT.NONE).setText("Faulted (7)");
		
		container.layout();
	}

	@Override
	public void setFocus() {
		// Empty
	}

	public IWorkitemFilter getFilter() {
		
		WorkitemFilter filter = new WorkitemFilter();
		
		if (dormantState.getSelection()) {
			filter.addRuntimeState(0);
		}
		
		if (acquiredState.getSelection()) {
			filter.addRuntimeState(1);
		}
		
		if (finishedState.getSelection()) {
			filter.addRuntimeState(2);
		}
		
		if (pausedState.getSelection()) {
			filter.addRuntimeState(3);
		}
		
		if (dpausedState.getSelection()) {
			filter.addRuntimeState(4);
		}
		
		if (apausedState.getSelection()) {
			filter.addRuntimeState(5);
		}
		
		if (ppausedState.getSelection()) {
			filter.addRuntimeState(6);
		}
		
		if (faultedState.getSelection()) {
			filter.addRuntimeState(7);
		}
		
		return filter;
	}
	
	
}
